/*******************************************************
> Copyright (C) 2024 ==IOT== All rights reserved.
> File Name: likang1.c
> Author: lv1
> Website:www.lv1.org
> Created Time: 2024年12月05日 星期四 16时38分00秒
***************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "cJSON.h"
#include <mysql/mysql.h>

char *packJson() {
    // 创建 JSON 对象
    cJSON* root = cJSON_CreateObject();

    char name[20] = {0};
    printf("input name: ");
    scanf("%s", name);
    cJSON* cjson_name = cJSON_CreateString(name);

    int age = 0;
    printf("input age: ");
    scanf("%d", &age);
    cJSON* cjson_age = cJSON_CreateNumber(age);

    char sex[5] = {0};
    printf("input sex: ");
    scanf("%s", sex);
    cJSON* cjson_sex = cJSON_CreateString(sex);

    char id_number[20] = {0};
    printf("input id_number: ");
    scanf("%s", id_number);
    cJSON* cjson_id_number = cJSON_CreateString(id_number);

    char family_members[20] = {0};
    printf("input family_members: ");
    scanf("%s", family_members);
    cJSON* cjson_family_members = cJSON_CreateString(family_members);

    char phone[15] = {0};
    printf("input phone: ");
    scanf("%s", phone);
    cJSON* cjson_phone = cJSON_CreateString(phone);

    char health_status[20] = {0};
    char pervalence[50] = {0};
    int medicine_fee = 0;
    printf("input health_status: ");
    scanf("%s", health_status); // 健康1，患病2
    cJSON* cjson_health_status = cJSON_CreateString(health_status);
	cJSON* cjson_pervalence;
	cJSON* cjson_medicine_fee;
    if (strcmp(health_status,"1") == 0) {
        cjson_pervalence = cJSON_CreateString("");
        cjson_medicine_fee = cJSON_CreateNumber(0);
    } else {
        printf("input pervalence: ");
        scanf("%s", pervalence);
        cjson_pervalence = cJSON_CreateString(pervalence);
        printf("input medicine_fee: ");
        scanf("%d", &medicine_fee);
        cjson_medicine_fee = cJSON_CreateNumber(medicine_fee);
    }

    cJSON* cjson_month_fee = cJSON_CreateNumber(2000);

    // 添加 JSON 对象
    cJSON_AddItemToObject(root, "name", cjson_name);
    cJSON_AddItemToObject(root, "age", cjson_age);
    cJSON_AddItemToObject(root, "sex", cjson_sex);
    cJSON_AddItemToObject(root, "id_number", cjson_id_number);
    cJSON_AddItemToObject(root, "family_members", cjson_family_members);
    cJSON_AddItemToObject(root, "phone", cjson_phone);
    cJSON_AddItemToObject(root, "health_status", cjson_health_status);
    cJSON_AddItemToObject(root, "pervalence", cjson_pervalence);
    cJSON_AddItemToObject(root, "medicine_fee",cjson_medicine_fee);
    cJSON_AddItemToObject(root, "month_fee", cjson_month_fee);

    char *p = (char *)malloc(1024);
	p = cJSON_Print(root);

	printf("%s\n",p);
	cJSON_Delete(root);
	return p;
}

void parseJson(char*p){
	cJSON* root = cJSON_Parse(p);
	cJSON *name = cJSON_GetObjectItem(root,"name");
	cJSON *age = cJSON_GetObjectItem(root,"age");
	cJSON *sex = cJSON_GetObjectItem(root,"sex");
	cJSON *id_number = cJSON_GetObjectItem(root,"id_number");
	cJSON *phone = cJSON_GetObjectItem(root,"phone");
	cJSON *family_members = cJSON_GetObjectItem(root,"family_members");
	cJSON *health_status = cJSON_GetObjectItem(root,"health_status");
	cJSON *pervalence = cJSON_GetObjectItem(root,"pervalence");
	cJSON *medicine_fee = cJSON_GetObjectItem(root,"medicine_fee");
	cJSON *month_fee = cJSON_GetObjectItem(root,"month_fee");
	
	char *name1 = cJSON_GetStringValue(name);
	ssize_t age1 = cJSON_GetNumberValue(age);
	char *sex1 = cJSON_GetStringValue(sex);
	char *id_number1 = cJSON_GetStringValue(id_number);
	char *phone1 = cJSON_GetStringValue(phone);
	char *family_members1 = cJSON_GetStringValue(family_members);
	char *health_status1 = cJSON_GetStringValue(health_status);
	ssize_t pervalence1 = cJSON_GetNumberValue(pervalence);
	ssize_t medicine_fee1 = cJSON_GetNumberValue(medicine_fee);
	char *month_fee1 = cJSON_GetStringValue(month_fee);
	
	printf("name:%s-age:%ld-sex:%s-id_number:%s-phone:%s-family_members:%s\
	-health_status:%s-pervalence:%s-medicine_feel:%ld-month_fee:%ld\n",\
	name1,age1,sex1,id_number1,phone1,family_members1,health_status1,pervalence1,medicine_fee1,month_fee1);
}

int main(int argc,const char* argv[])
{
	int fp = socket(AF_INET,SOCK_DGRAM,0);
	
	char*p = packJson();
	parseJson(p);
	free(p);
	close(fp);
	
    return 0;
}